    <div class="head"><h5 class="iList">User data</h5></div>
     <form name="user_form" action="<?php echo url_for('users/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight">
     		<span style="margin-right:20px;">
                  <input id="user_name" type="text" name="user[name]" value="<?php echo $form->getObject()->getName()?>">
            </span>
			</div>
     		<div class="fix"></div>
     	</div>
     	
     	<div class="rowElem noborder">
     	<label for="name">Login name:</label>
     		<div class="formRight">
     		<span style="margin-right:20px;">
                  <input id="user_login" type="text" name="user[login]" value="<?php echo $form->getObject()->getLogin()?>">
            </span>
			</div>
     		<div class="fix"></div>
     	</div>     	
     	
     	<div class="rowElem noborder">
     	<label for="name">Password:</label>
     		<div class="formRight">
     		<span style="margin-right:20px;">
                  <input id="the_user_password" type="password" name="the_user_password" value="">
            </span>
			</div>
     		<div class="fix"></div>
     	</div>      	
     	
     	<div class="rowElem noborder">
     	<label for="name">Repeat password:</label>
     		<div class="formRight">
     		<span style="margin-right:20px;">
                  <input id="repeat_password" type="password" name="repeat_password" value="">
            </span>
			</div>
     		<div class="fix"></div>
     	</div>      	
     	
     	
     	<div class="rowElem noborder">
     	<label for="name">Rol:</label>
     		<div class="formRight">
     		<span style="margin-right:20px;">
                  <select id="user_user_rol_id"  name="user[user_rol_id]" style="width:100px;">
                  	<?php foreach ($roles as $rol){?>
                  	<?php $selected = $rol->getId() == $form->getObject()->getUserRolId() ? 'selected' : ''?>
                  	<option <?php echo $selected?> value="<?php echo $rol->getId()?>"> <?php echo $rol->getName()?> </option>
                  	<?php }?>
                  </select>
            </span>
			</div>
     		<div class="fix"></div>
     	</div>           	
     	
     	<div class="rowElem noborder">
     	<label for="name">Directories:</label>
     		<div class="formRight" style="float:left; width:800px">
     		<span style="margin-right:20px;">
                 <?php 
                 $c = 0;
                 foreach ($directories as $directory){
                 $c++;
                 ?>
                 <?php  
                 		$set_user_name = "";
                 		$set_user = $directory->getUser();
                 		if($set_user->getId() != null){
                 			$set_user_name = " <i>(asigned to <b>" . $set_user->getName() . "</b>)</i>";
                 		}
                 		
                 		$selected_directories_ids_int = array();
                 		foreach ($selected_directories_ids as $id){
                 			array_push($selected_directories_ids_int, $id);
                 		}
                 		
                 		
                 		$selected = in_array($directory->getId(), $selected_directories_ids_int) ? 'checked=true' : '';
                 ?>
                 <div style="float:left; width: 380px; margin-right:10px">
                	<div style="float:left;"> <input type="checkbox" name="directories[]" <?php echo $selected?> value="<?php echo $directory->getId()?>"></div> <div style="float:left;margin-left: 5px; margin-top: 4px;"> <?php echo $directory->getName() . $set_user_name?> </div>
                 </div>	
                 <?php if ($c % 2 == 0){?>
                 	<div class="fix"></div>
                 	<?php }?>
                 <?php }?>
            </span>
			</div>
     		<div class="fix"></div>
     	</div>        	
     	
     	
     	<div class="fix"></div>
        <input type="button" value="Save" class="greyishBtn submitForm" id="filter" />
   
        <div class="fix"></div>
     </form>
     
     
<script type="text/javascript">

$("#filter").click(function (event){
    submitForm();
})





$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:'<?php echo url_for('users/' . ($form->getObject()->isNew() ? 'create' :'update') . (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
        beforeSend: function(){
    		if (!validate()){
				return false;
        	}
        },
        
        success: function(data) {
             $('#category_container_div').html(data);
        },
        error: function(){
    		$('#msg_text').html("A user with the selected login already exists, please choose a new one");
    		$('#error_msg').show('fast');
        }

   });
    
}


function validate(){
	var valid = true;
	var error_msg = "";
	if ($("#user_name").attr("value") == ""){
		valid = false;
		error_msg = "Name required";
	}
	else if (valid && $("#user_login").attr("value") == ""){
		valid = false;
		error_msg = "Login required";
	}
	<?php if ($form->getObject()->isNew()){?>
	else if (valid && $("#the_user_password").attr("value") == ""){
		valid = false;
		error_msg = "Password required";
	}
	<?php }?>
	else if (valid && $("#the_user_password").attr("value") != $("#repeat_password").attr("value")){
		valid = false;
		error_msg = "Passwords must match";
	}	
		
	if (!valid){
		$('#msg_text').html(error_msg);
		$('#error_msg').show('fast');
	}
	return valid;			
}


		
		$('form').jqTransform({imgPath:'../images/forms'});
</script>     
